const mysql = require('mysql');

// 建立链接
const connection = mysql.createConnection({
    host: 'localhost', //本地数据库的链接，也就是地址
    port: 3306,
    user: 'root', //mysql设置的用户名
    password: '123456789', //密码
    database:'shop',//要连接地址所在的那个数据库
});

connection.connect(error => {
    if (error) throw error;
    console.log('数据库连接成功');
});

const query = (sql, parmas = null) => {
// 获取数据库链接对象
    return new Promise(function (reject, resolve) {
        // 执行SQL语句
        connection.query(sql, parmas, function (error, results, fields) {
            if (error) throw error;
            reject(results);
        });
        // 关闭链接
        connection.end(error => {
            if (error) throw error;
            console.log('数据库关闭链接');
        });
    });
};


module.exports = {
    query
};